<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 200px;
            height: 200px;
            background: aquamarine;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>

</body>
<script>
    var box = document.getElementsByClassName('box')[0]
    var x = null
    var y = null

    box.addEventListener('mousedown', mouseThing)
    // box.onmousedown = function (e) {
    //     var e = e || window.event
    //     var x = e.pageX - box.offsetLeft
    //     var y = e.pageY - box.offsetTop
    //     console.log(x, y)
    //     document.onmousemove = function (e) {
    //         var X = e.pageX - x
    //         var Y = e.pageY - y
    //         box.style.left = X + 'px'
    //         box.style.top = Y + 'px'
    //     }
    //     box.onmouseup = function () {
    //         document.onmousemove = null
    //     }
    // }
    function mouseThing(e) {
        var e = e || window.event
        if (e.type == "mousedown") {
            x = e.pageX - box.offsetLeft
            y = e.pageY - box.offsetTop
            document.addEventListener('mousemove', mouseThing)
            box.addEventListener('mouseup', mouseThing)
        } else if (e.type == 'mousemove') {
            var X = e.pageX - x
            var Y = e.pageY - y
            box.style.left = X + 'px'
            box.style.top = Y + 'px'
        } else if (e.type == 'mouseup') {
            document.removeEventListener('mousemove', mouseThing)
        }
    }

</script>

</html>